import type { ReactElement, ReactNode, MutableRefObject } from 'react';
import type { TableProps, TablePaginationConfig } from 'antd/es/table';
import type { ColumnType } from 'antd/lib/table/interface';
import type { DictType } from '@/components/DictTag';
import type { TreeSelectProps, FormInstance } from 'antd';

export type AnyObject = Record<PropertyKey, any>;

export type SearchItemType =
	| 'Input'
	| 'Select'
	| 'DatePicker'
	| 'DateRangePicker'
	| 'TimePicker'
	| 'TreeSelect';
export type SearchConfig = {
	label: string;
	name: string;
	component: SearchItemType;
	enum?: DictType[];
	treeData?: TreeSelectProps['treeData'];
	startKeyName?: string;
	endKeyName?: string;
}[];

export interface NewColumnType<R = any> extends ColumnType<R> {
	maxWidth?: number;
	clamp?: number;
}
export type TableProRef = { reload: () => void };

type Renders<R> = {
	[k: string]: ColumnType<R>['render'];
};

export type RequestFn<R, P> = (
	pageInfo: { pageNo: number; pageSize: number },
	param: P
) => Promise<{ list: R[]; total?: number }>;
export interface MainTableProps<R = AnyObject, P = AnyObject>
	extends Omit<TableProps<R>, 'ref' | 'components' | 'loading' | 'scroll' | 'onChange'> {
	/**
	 * @desc 表格名称
	 */
	tableName?: ReactNode;
	/**
	 * @desc 表格高度
	 */
	tableHeight?: number;
	/**
	 * @desc Table Y轴可滑动高度
	 */
	scrollHeight?: number;
	/**
	 * @desc 工具栏按钮
	 */
	toolbar?: ReactElement;
	/**
	 * @desc 工具栏两侧内边距
	 */
	toolbarPadding?: boolean;
	/**
	 * @param show 是否显示序号列
	 * @param type default: 每页从1开始；increase: 根据页码计算
	 */
	index?: { show?: boolean; type?: 'default' | 'increase' }; // 是否开启序号列，默认开启
	/**
	 * @desc 列配置
	 */
	columns?: TableColumns;
	/**
	 * @desc columns的自定义render
	 */
	renders?: Renders<R>;
	/**
	 * @desc 分页配置
	 */
	pagination?: Omit<TablePaginationConfig, 'defaultPageSize'> | false;
	/**
	 * @desc 筛选框form实例，用于自定义表单值
	 */
	formRef?: MutableRefObject<FormInstance>;
	/**
	 * Table组件实例
	 */
	tableRef?: MutableRefObject<TableProRef>;
	/**
	 * @desc 筛选框form表单项配置
	 */
	search?: SearchConfig;
	formConfig?: { labelWidth?: number };
	/**
	 * @desc 查询列表数据的方法
	 * @param pageInfo 分页信息
	 * @param param 查询条件
	 * @returns { data: [], total: 0 }
	 */
	request?: RequestFn<R, P>;
	/**
	 * @desc 点击筛选框搜索按钮时的回调
	 * @param data 筛选参数
	 */
	onSearchFormChange?: (data: any) => void;
	/**
	 * @desc 切换页码时的回调
	 * @param pageNo 页码
	 * @param pageSize 分页大小
	 */
	onPageChange?: (pageNo: number, pageSize: number) => void;
}

export type TableRenders<RecordType = any> = Renders<RecordType>;

export interface NewColumnType<R = any> extends ColumnType<R> {
	maxWidth?: number;
	clamp?: number;
}

export type TableColumns<RecordType = any> = NewColumnType<RecordType>[];

export type PageLayoutProps = {
	table?: ReactNode;
	search?: ReactNode;
};
